package com.gouaixin.bee.beeframework2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.Gift;
import com.gouaixin.bee.beeframework2.tool.mLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/3/30.
 */
public class E1_GiftShopAdapter extends BaseAdapter {

    Context context;
    List<Gift> datas = new ArrayList<Gift>();
    int numColumns;
    int add;
//    ImageLoader imageLoader;

    public E1_GiftShopAdapter(Context context) {
        this.context = context;
//        imageLoader=ImageLoader.getInstance();
    }

    public void setData(List<Gift> data,int numColumns) {
        datas = data;
        this.numColumns=numColumns;
    }

    @Override
    public int getCount() {
        int number=datas.size();
        if(number%numColumns==0){
            return number;
        }else {
            //多余项
            int temp=number%numColumns;
            //增加空白项
            int  add=numColumns-temp;
            return datas.size()+add;
        }

    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
//        if(convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.item_gift,null);

            AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    160);
            convertView.setLayoutParams(param);

            viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.price = (TextView) convertView.findViewById(R.id.price);
            viewHolder.balanceBg = (LinearLayout) convertView.findViewById(R.id.balanceBg);
            convertView.setTag(viewHolder);
//        }else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
        if(position<datas.size()){
            Gift bean=datas.get(position);
            int icon=bean.getIcon();
            mLog.e("bean.getIcon():适配器中=" + icon);
            String name=bean.getName();
            String price=bean.getPrice();

            try{
                viewHolder.icon.setImageResource(icon);
            }catch (Exception e){
                mLog.e("出错原因：" + e.getMessage());
            }

            viewHolder.name.setText(name);
            viewHolder.price.setText(price);
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.name.setVisibility(View.VISIBLE);
            viewHolder.price.setVisibility(View.VISIBLE);
            viewHolder.balanceBg.setVisibility(View.VISIBLE);
        }

        return convertView;
    }


    /***
     * 视图控制类
     */
    private class ViewHolder{
        ImageView icon;
        TextView name;
        TextView price;
        LinearLayout balanceBg;
    }

}
